/**
 * 
 */
package com.my.test.testnet.talk;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * create date: 2010-10-22 上午09:22:42
 * @author Angela Tao
 * @version 1.0
 *
 */
public class TalkClient extends KeyAdapter implements ActionListener,Runnable{
	
	private JFrame f = new JFrame("客户端");
	private JTextArea ta = new JTextArea();
	private JScrollPane sp = new JScrollPane(ta);
	private JTextField tf = new JTextField(20);
	private JButton b = new JButton("发送");
	private JPanel p = new JPanel();

	private DataInputStream dis;
	private DataOutputStream dos;
	
	private String name;
	private String ip;
	/**
	 * 
	 */
	public TalkClient(){
		
	}
	
	public TalkClient(String name,String ip) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.ip = ip;
		
		f.setTitle(name);
		ta.setEditable(false);
		p.add(tf);
		p.add(b);
		f.add(p,"South");
		f.add(sp);
		
		f.setSize(350, 400);
		f.setLocation(200, 200);
		f.setVisible(true);
		
		b.addActionListener(this);
		tf.addKeyListener(this);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.connect();
		new Thread(this).start();
	}
	
	public void connect(){
		Socket s = null;
		try {
			s = new Socket(ip,6000);
			
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		/*
		finally {
			try {
				if(s != null){
					s.close();
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		*/
	}

	/**
	 * @param args
	 */
	/*
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TalkClient();
	}
	*/
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String info = tf.getText();
		tf.setText("");
		ta.append(name+": "+info+"\n");
		
		try {
			dos.writeUTF(name+": "+info);
			dos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while(true){
				String info = dis.readUTF();
				ta.append(info+"\n");
			}	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void keyPressed(KeyEvent e){
		if(e.getKeyCode() == 10){
			String info = tf.getText();
			tf.setText("");
			ta.append("Client: "+info+"\n");
			
			try {
				dos.writeUTF("Client: "+info);
				dos.flush();
			} catch (IOException ee) {
				// TODO Auto-generated catch block
				ee.printStackTrace();
			}
		}
	}

}
